package com.ourteam.frame.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import com.ourteam.frame.exception.BaseAppException;

public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory.getLog(CustomSimpleMappingExceptionResolver.class);

	@Override
	protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		// Expose ModelAndView for chosen error view.
		String viewName = determineViewName(ex, request);
		logger.error("ResolveException : ", ex);
		if (viewName != null) {
			if ((request.getHeader("X-Requested-With") == null)) {// 普通视图返回
				// 如果不是异步请求
				// Apply HTTP status code for error views, if specified.
				// Only apply it if we're processing a top-level request.
				Integer statusCode = determineStatusCode(request, viewName);
				if (statusCode != null) {
					applyStatusCodeIfPossible(request, response, statusCode);
				}
				return getModelAndView(viewName, ex, request);
			} else {// JSON格式返回
				response.setStatus(500);
				ModelAndView mav = new ModelAndView();
				MappingJackson2JsonView view = new MappingJackson2JsonView();
				Map<String, Object> result = new HashMap<String, Object>();
				result.put("code", -1);
				result.put("message", ex.getMessage());
				view.setAttributesMap(result);
				mav.setView(view);
				return mav;
			}
		} else {
			return null;
		}
	}

	@Override
	protected ModelAndView getModelAndView(String viewName, Exception ex) {
		ModelAndView mv = new ModelAndView(viewName);
		if (logger.isDebugEnabled()) {
			logger.debug("Exposing Exception as model attribute '" + "exception" + "'");
		}
		if (ex instanceof BaseAppException) {
			mv.addObject("code", ((BaseAppException) ex).getCode());
			mv.addObject("message", ((BaseAppException) ex).getDesc());
		} else if (ex instanceof AuthorizationException) {
			mv.addObject("message", ex.getMessage());
		} else {
			mv.addObject("code", "内部错误");
			mv.addObject("message", ex.getMessage());
		}
		return mv;
	}
}
